﻿namespace NEclipse
{
  using System;
  using System.IO;
  using System.Windows.Forms;
  using Properties;

  /// <summary>
  /// Implementation of the workspcae launcher window.
  /// </summary>
  public partial class WorkspaceLauncher : Form
  {
    /// <summary>
    /// Delimiter to load/store workspace paths as user setting.
    /// </summary>
    private const char WorkspaceDelimiter = ';';

    /// <summary>
    /// Initializes a new instance of the <see cref="WorkspaceLauncher"/> class.
    /// </summary>
    public WorkspaceLauncher()
    {
      InitializeComponent();
      string[] usedWorkspaces = Settings.Default.Startup_UsedWorkspaces.Split(new[] { WorkspaceDelimiter }, StringSplitOptions.RemoveEmptyEntries);
      this.comboBoxWorkspaces.Items.AddRange(usedWorkspaces);
      if (this.comboBoxWorkspaces.Items.Contains(Settings.Default.Startup_SelectedWorkspace))
      {
        this.comboBoxWorkspaces.SelectedItem = Settings.Default.Startup_SelectedWorkspace;
      }
    }

    /// <summary>
    /// Gets the workspace directory.
    /// </summary>
    public string WorkspaceDirectory
    {
      get
      {
        return this.comboBoxWorkspaces.SelectedItem.ToString();
      }
    }

    /// <summary>
    /// Handles the Click event of the buttonBrowse control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    private void buttonBrowse_Click(object sender, EventArgs e)
    {
      FolderBrowserDialog fbd = new FolderBrowserDialog();
      if (fbd.ShowDialog() == DialogResult.OK)
      {
        if (comboBoxWorkspaces.Items.Contains(fbd.SelectedPath) == false)
        {
          comboBoxWorkspaces.Items.Add(fbd.SelectedPath);
        }

        comboBoxWorkspaces.SelectedItem = fbd.SelectedPath;
      }

      fbd.Dispose();
    }

    /// <summary>
    /// Handles the Click event of the buttonOk control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    private void buttonOk_Click(object sender, EventArgs e)
    {
      Settings.Default.Startup_SelectedWorkspace = this.comboBoxWorkspaces.Text;
      string workspaceList = string.Empty;
      foreach (string item in this.comboBoxWorkspaces.Items)
      {
        workspaceList += item + WorkspaceDelimiter;
      }

      workspaceList = workspaceList.Substring(0, workspaceList.Length - 1);
      Settings.Default.Startup_UsedWorkspaces = workspaceList;
      Settings.Default.Save();      
    }

    /// <summary>
    /// Handles the TextChanged event of the comboBoxWorkspaces control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    private void comboBoxWorkspaces_TextChanged(object sender, EventArgs e)
    {
      if (this.comboBoxWorkspaces.Text != null)
      {
        if (Directory.Exists(this.comboBoxWorkspaces.Text))
        {
          this.buttonOk.Enabled = true;
        }
        else
        {
          this.buttonOk.Enabled = false;
        }
      }
    }
  }
}
